using System;
using ETModel;

namespace ETHotfix
{
    public static class LoginHelper
    {
        public static async ETVoid OnLoginAsync(string userAccount,string password,Action<string> errCallback)
        {
            try
            {

                if (string.IsNullOrEmpty(userAccount)) {
                    errCallback("请输入用户名");
                    return;
                }
                if (string.IsNullOrEmpty(password)) {
                    errCallback("请输入密码");
                    return;
                }

                // 创建一个ETModel层的Session
                ETModel.Session session = ETModel.Game.Scene.GetComponent<NetOuterComponent>().Create(GlobalConfigComponent.Instance.GlobalProto.Address);
                Log.Info("R服务器地址:" + GlobalConfigComponent.Instance.GlobalProto.Address + ",解析后:" + session.RemoteAddress.ToString());

                // 创建一个ETHotfix层的Session, ETHotfix的Session会通过ETModel层的Session发送消息
                Session realmSession = ComponentFactory.Create<Session, ETModel.Session>(session);
                R2C_Login r2CLogin = (R2C_Login)await realmSession.Call(new C2R_Login { UserAccount = userAccount, Password = password });
                realmSession.Dispose();
                if (r2CLogin.Error != 0) {
                    errCallback(r2CLogin.Message);
                    return;
                }

                // 创建一个ETModel层的Session,并且保存到ETModel.SessionComponent中
                ETModel.Session gateSession = ETModel.Game.Scene.GetComponent<NetOuterComponent>().Create(r2CLogin.GateAddress);
                var gateSessionComp = ETModel.Game.Scene.GetComponent<ETModel.SessionComponent>();
                if (gateSessionComp == null) gateSessionComp = ETModel.Game.Scene.AddComponent<ETModel.SessionComponent>();
                gateSessionComp.Session = gateSession;

                // 创建一个ETHotfix层的Session, 并且保存到ETHotfix.SessionComponent中
                var hotfixSessionComp = Game.Scene.GetComponent<SessionComponent>();
                if (hotfixSessionComp == null) hotfixSessionComp = Game.Scene.AddComponent<SessionComponent>();
                hotfixSessionComp.Session = ComponentFactory.Create<Session, ETModel.Session>(gateSession);

                G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await SessionComponent.Instance.Session.Call(new C2G_LoginGate { UserAccount = userAccount, GateLoginKey = r2CLogin.GateLoginKey });
                if (g2CLoginGate.Error != 0) {
                    errCallback(g2CLoginGate.Message);
                    return;
                }

                Log.Info("登陆gate成功!");

                // 创建Player
                Player player = ComponentFactory.CreateWithId<Player>(g2CLoginGate.PlayerId);
                PlayersComponent playerComponent = Game.Scene.GetComponent<PlayersComponent>();
                playerComponent.MyPlayer = player;

                Game.EventSystem.Run(HotfixEventIdType.LoginFinish);
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        } 
    }
}